<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 600px;
            margin: 100px auto;
        }
        
        .msg {
            display: inline-block;
            font-size: 12px;
            color: #999;
            background: url(img/mess.png) no-repeat left center;
            padding-left: 20px;
        }
        
        .wrong {
            color: red;
            background: url(img/wrong.png) no-repeat left center;
        }
        
        .right {
            color: green;
            background: url(img/right.png) no-repeat left center;
        }
    </style>
</head>

<body>
    <div>
        <input type="password" class="pwd">
        <p class="msg">6~16位密码</p>
    </div>
    <script>
        var input = document.querySelector(".pwd");
        var msg = document.querySelector(".msg");

        input.onblur = function() {
            if (this.value.length < 6 || this.value.length > 16) {
                msg.innerHTML = "密码超出范围";
                msg.className = "msg wrong";
            } else {
                msg.innerHTML = "密码正确"
                msg.className = "msg right"
                
            }
        }
        input.onkeydown = function() {
            if (this.value.length < 6 || this.value.length > 16) {
                msg.innerHTML = "密码超出范围";
                msg.className = "msg wrong";
            } else {
                msg.innerHTML = "密码正确"
                msg.className = "msg right"
            }
        }
    </script>
</body>

</html>